// i.r(t), i.d(t, { default: () => DwellAnalytics })
import Module from "./Module"
import AnalyticsModule from "./AnalyticsModule"
import CameraData from "../data/CameraData"
import ApplicationData from "../data/ApplicationData"
import ViewmodeData from "../data/ViewmodeData"
import { dateISO } from "../utils/date.util"
import u from "../math/19674"
import { phaseMin } from "../enum/phase.enum"
import funcUtil from "../utils/func.util"
import viewmodeEnum from "../enum/viewmode.enum"
import { getViewName } from "../utils/viewmode.util"
function g(e) {
  const t = funcUtil.getXYZW(u.ep.toVisionQuaternion(e.rotation))
  return {
    position: funcUtil.copyPostion(u.ep.toVisionVector(e.position)),
    rotation: t,
    aspect: e.aspect(),
    isOrtho: e.isOrtho(),
    fovX: e.fovX(),
    fovY: e.fovY()
  }
}
function m(e) {
  return dateISO(new Date(e))
}
export default class DwellAnalytics extends Module {
  pendingDwellEvents: any[]
  currentEvent: any
  appData: any
  analytics: any
  cameraData: any
  viewmodeData: any
  resumeTrackingDwellTime: any
  constructor() {
    super(...arguments), (this.name = "dwell-analytics"), (this.pendingDwellEvents = [])
    this.resumeTrackingDwellTime = () => {
      this.currentEvent.startTimeMs || (this.currentEvent.startTimeMs = Date.now())
      this.scheduleDwellTimeOutEvent()
    }
  }
  get onCameraChange() {
    return funcUtil.throttle(e => {
      this.checkForDwellEvent()
      e.clone(this.currentEvent.pose)
      this.currentEvent.startTimeMs = Date.now()
      this.scheduleDwellTimeOutEvent()
    }, 1000)
  }
  checkForDwellEvent(e = !1) {
    const { startTimeMs: t, viewmode: i } = this.currentEvent
    if (!t || this.appData.phase !== phaseMin.PLAYING) return
    const s = Date.now()
    const n = s - t
    const r = {
      pose: g(this.currentEvent.pose),
      durationMs: n,
      startTime: m(t),
      endTime: m(s),
      timedOut: e,
      viewmode: i
    }
    this.trackDwellEvent(r)
  }
  onViewmodeUpdate(e) {
    e.value !== viewmodeEnum.Transition && (this.currentEvent.viewmode = getViewName(e.value))
  }
  trackDwellEvent(e) {
    this.pendingDwellEvents.push(e)
    this.trackPendingDwellEvents()
  }
  get trackPendingDwellEvents() {
    return funcUtil.throttle(() => {
      const e = { events: this.pendingDwellEvents }
      this.pendingDwellEvents = []
      this.analytics.track("dwell_events", e)
    }, 5000)
  }
  onBlur() {
    return this.stopTrackingDwellTime(!1)
  }
  stopTrackingDwellTime(e = !1) {
    this.checkForDwellEvent(e)
    delete this.currentEvent.startTimeMs
  }

  get scheduleDwellTimeOutEvent() {
    return funcUtil.debounce(() => this.stopTrackingDwellTime(!0), 15000)
  }
  get throttledResumeTrackingDwellTime() {
    return funcUtil.throttle(this.resumeTrackingDwellTime, 100)
  }
  async init(e, t) {
    ;[this.analytics, this.cameraData, this.appData, this.viewmodeData] = await Promise.all([
      t.getModule(AnalyticsModule),
      t.market.waitForData(CameraData),
      t.market.waitForData(ApplicationData),
      t.market.waitForData(ViewmodeData)
    ])
    this.currentEvent = {
      pose: this.cameraData.pose.clone(),
      viewmode: getViewName(null)
    }
    this.bindings.push(this.cameraData.pose.onChanged(this.onCameraChange.bind(this)))
    this.bindings.push(this.viewmodeData.onPropertyChanged("currentModeObservable", this.onViewmodeUpdate.bind(this)))
    window.addEventListener("blur", this.onBlur.bind(this))
    window.addEventListener("focus", this.resumeTrackingDwellTime)
    window.addEventListener("keydown", this.resumeTrackingDwellTime)
    window.addEventListener("touchcancel", this.resumeTrackingDwellTime)
    window.addEventListener("touchstart", this.resumeTrackingDwellTime)
    window.addEventListener("touchend", this.resumeTrackingDwellTime)
    window.addEventListener("touchmove", this.throttledResumeTrackingDwellTime.bind(this))
    window.addEventListener("mousemove", this.throttledResumeTrackingDwellTime.bind(this))
  }
  dispose(e) {
    super.dispose(e),
      window.removeEventListener("blur", this.onBlur.bind(this)),
      window.removeEventListener("focus", this.resumeTrackingDwellTime),
      window.removeEventListener("keydown", this.resumeTrackingDwellTime),
      window.removeEventListener("touchcancel", this.resumeTrackingDwellTime),
      window.removeEventListener("touchstart", this.resumeTrackingDwellTime),
      window.removeEventListener("touchend", this.resumeTrackingDwellTime),
      window.removeEventListener("touchmove", this.throttledResumeTrackingDwellTime.bind(this)),
      window.removeEventListener("mousemove", this.throttledResumeTrackingDwellTime.bind(this))
  }
}
